<?php
class Menu extends CI_Controller
{
	public function __construct ()
	{
		parent::__construct();
		$this->template->set_template('template/admin_template');
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
	}
	public function index()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'Menu'));
        $data['menus'] = $this->general_model->select_rows('menu','*','menu_name','asc','10');
		
		$this->template->content->view('admin/menu/menu', $data);
		$this->template->publish();
	}
	public function addmenu()
	{
		$data['breadcrumb'] = set_crumbs(array('menu' => 'Menus', current_url() => 'Add Menu'));
		
		$this->form_validation->set_rules('menu_name', 'Menu name', 'trim|required|max_length[255]');

        // Form validation
        if ($this->form_validation->run() == TRUE)
        {
            $menudetails=array(
					'menu_name'=>$this->input->post('menu_name'),
					'required'=>1);
			$menuid=$this->general_model->insert('menu',$menudetails);
            $this->session->set_flashdata('message', '<p class="success">Menu Saved.<p>');

            redirect(ADMIN_PATH . '/menu/tree/' . $menuid);
        }
		$this->template->content->view('admin/menu/addmenu', $data);
		$this->template->publish();
	}
	public function editmenu()
	{
		$data['breadcrumb'] = set_crumbs(array('menu' => 'Menus', current_url() => 'Edit Menu'));
		$menuid=$this->uri->segment(4);
		//echo $pageid;
		$this->form_validation->set_rules('menu_name', 'Menu name', 'trim|required|max_length[255]');

        // Form validation
        if ($this->form_validation->run() == TRUE)
        {
            $menudetails=array(
					'menu_name'=>$this->input->post('menu_name'));
			$menuid=$this->general_model->update('menu',$menudetails,'menu_id = '.$menuid);
            $this->session->set_flashdata('message', '<p class="success">Menu Renamed Successfully.<p>');

            redirect(ADMIN_PATH . '/menu');
        }
		$data['menudetails']=$this->general_model->select_val("menu","*",'menu_id = '.$menuid);
		$this->template->content->view('admin/menu/editmenu', $data);
		$this->template->publish();
	}
	public function deletemenu()
	{
		if ($this->input->post('selected'))
        {
            $selected = $this->input->post('selected');
        }
        else
        {
            $selected = (array) $this->uri->segment(5);
        }
		$this->general_model->delete_rows('menu','menu_id',$selected);
		$this->session->set_flashdata('message', '<p class="success">Menu deleted Successfully.</p>');
		redirect(ADMIN_PATH . "/menu");
	}
	public function tree()
	{
		$data['breadcrumb'] = set_crumbs(array('menu' => 'Menus', current_url() => 'Menu Tree'));
		$this->template->stylesheet->add(base_url('assets/admin/js/nested_sortable/jquery.ui.nestedSortable.css'));
		$this->template->javascript->add(base_url('assets/admin/js/nested_sortable/jquery.ui.nestedSortable.js'));
		$data['menuid']=$menu_id = $this->uri->segment(4);
		/*global $treeview;
	  
		$pid =$member['employee_id'];
		
		$treeview.="<ul id='org' style='display:none'><li><a>".$member['employee_name']."</a><ul>";
		$this->geneology($pid);
		$treeview.="</ul></li></ul>";
		
		$data=array("treeview"=>$treeview,
					"member"=>$member);
		
		$this->template->content->view('salesgenelogyview',$data);
		// publish the template
		$this->template->publish();*/
		//$menulist =$this->general_model->select_ord("menu_items","*","sort_order","ASC","menu_id ='".$menu_id."'");
		//$menulist =$this->general_model->get_jointable("menu_items","pages","menu_items.*,pages.slug,pages.title","menu_items.pageid=pages.pageid","menu_items.menu_id ='".$menu_id."' order by menu_items.sort_order asc");
		$menulist =$this->general_model->select("menu_items","*","menu_id ='".$menu_id."' order by sort_order asc");
					
		$treelist =get_nested($menulist);
		/*echo "<pre>";
		print_r($treelist);
		exit;*/
		$data['tree']=list_admin_nav($treelist);
		//print_r($data['tree']);
		$this->template->content->view('admin/menu/tree', $data);
		$this->template->publish();
	}
	
	function savetree($menuid,$listmenu=array())
    {
       
		if(!empty($listmenu))
		{
			$lists = $listmenu;
		}
		else
		{
			$lists = $_POST['list'];
		}
		/*echo "<pre>";
		print_r($lists);*/
		
        $i = 1;
        foreach($lists as $id=>$parent_id)
        {
            
			
            $node_info_array = array();
            $node_info_array['id'] = $id;
            $node_info_array['parent_id'] = ($parent_id == 'root') ? 0 : $parent_id;
			$node_info_array['sort_order'] = $i;
			$data['menulist'] =$this->general_model->update("menu_items",$node_info_array,"id ='".$id."'");
           
            $i++;
        }
		//$data['menulist'] =$this->general_model->update("menu_items",$node_info_array,"id ='".$id."'");
		//print_r($node_info_array);
       
    }
	function additems()
    {
        // Init
        $data['menuid'] = $menuid = $this->uri->segment(4);
        $data['breadcrumb'] = set_crumbs(array('menu' => 'Menus', 'menu/tree/' . $menuid => 'Menu Tree', current_url() => 'Add Items'));
		$data['pages'] = $this->general_model->select("pages","pageid,title,url","status = 'published'");
		$menulist = $this->general_model->select("menu_items","*","menu_id =".$menuid." order by sort_order asc");
		$parentlist =get_nested($menulist);
		//echo "<pre>";
		//print_r($data['parentpages']);
		$data['parentpages']=list_select_nav($parentlist);
		if ($this->input->post('page_type') == 'url')
        {
            $this->form_validation->set_rules('title', 'Link Text', 'trim|required');
            $this->form_validation->set_rules('url', 'URL', 'trim|required');
        }
        $this->form_validation->set_rules('class', 'Class', 'trim');
        $this->form_validation->set_rules('target', 'Target', 'trim');
		if ($this->form_validation->run() == TRUE)
        {
			$sort=$this->general_model->select_val("menu_items","max(sort_order)","menu_id =".$menuid);
			
			if ($this->input->post('page_type') == 'page')
            {
                $pageid = $this->input->post('page_id');
                $url = $this->input->post('pageurl');
				$title=$this->input->post('pagetitle');
				
            }
            else if ($this->input->post('page_type') == 'url')
            {
				$pageid=0;
                //$url = base_url().trim($this->input->post('url'), '/');
				$url = $this->input->post('url');
				$title = $this->input->post('title');
            }
			$menuitem=array(
					"menu_id"=>$menuid,
					"pageid"=>$pageid,
					"page_type"=>$this->input->post('page_type'),
					"title"=>$title,
					"url"=>$url,
					"class"=>$this->input->post('class'),
					"parent_id"=>$this->input->post('parent_id'),
					"target"=>$this->input->post('target'),
					"sort_order"=>$sort['max(sort_order)']+1
			);
			
            $this->general_model->insert("menu_items",$menuitem);
			/*update sort order*/
			$sortorderlist = $this->general_model->select("menu_items","*","menu_id =".$menuid." order by sort_order asc");
			$list=array();
			foreach($sortorderlist as $orderlist)
			{
				if($orderlist['parent_id']==0)
				{
					$parent_id='root';
				}
				else
				{
					$parent_id=$orderlist['parent_id'];
				}
				$lists[$orderlist['id']]=$parent_id;
				
			}
			$this->savetree($menuid,$lists);
            $this->session->set_flashdata('message', '<p class="success">Navigation Item Saved</p>');
            redirect(ADMIN_PATH . "/menu/tree/".$menuid);
        }

        $this->template->content->view('admin/menu/additems', $data);
		$this->template->publish();
    }
	public function edititems()
	{
		$data['menuid'] = $menuid = $this->uri->segment(4);
		$data['itemid'] = $itemid = $this->uri->segment(5);
        $data['breadcrumb'] = set_crumbs(array('menu' => 'Menus', 'menu/tree/' . $menuid => 'Menu Tree', current_url() => 'Edit Items'));
		$data['pages'] = $this->general_model->select("pages","pageid,title,url","status = 'published'");
		
		if ($this->input->post('page_type') == 'url')
        {
            $this->form_validation->set_rules('title', 'Link Text', 'trim|required');
            $this->form_validation->set_rules('url', 'URL', 'trim|required');
        }
        $this->form_validation->set_rules('class', 'Class', 'trim');
        $this->form_validation->set_rules('target', 'Target', 'trim');
		if ($this->form_validation->run() == TRUE)
        {
			
			if ($this->input->post('page_type') == 'page')
            {
                $pageid = $this->input->post('page_id');
                $url = $this->input->post('pageurl');
				$title=$this->input->post('pagetitle');
				
            }
            else if ($this->input->post('page_type') == 'url')
            {
				$pageid=0;
                //$url = base_url().trim($this->input->post('url'), '/');
				$url = $this->input->post('url');
				$title = $this->input->post('title');
            }
			$menuitem=array(
					"pageid"=>$pageid,
					"page_type"=>$this->input->post('page_type'),
					"title"=>$title,
					"url"=>$url,
					"class"=>$this->input->post('class'),
					"target"=>$this->input->post('target')
			);
			
            $this->general_model->update("menu_items",$menuitem,"id = ".$itemid);
            $this->session->set_flashdata('message', '<p class="success">Navigation Item Updated</p>');
            redirect(ADMIN_PATH . "/menu/tree/".$menuid);
        }
		$data['itemdetails']=$this->general_model->select_val("menu_items","*",'id = '.$itemid);
		
        $this->template->content->view('admin/menu/edititems', $data);
		$this->template->publish();
	}
	public function deleteitems()
	{
		$menuid = $this->uri->segment(4);
		$itemid = $this->uri->segment(5);
		$this->general_model->delete_row('menu_items','id ='.$itemid);
		$this->session->set_flashdata('message', '<p class="success">Item deleted Successfully.</p>');
		redirect(ADMIN_PATH . "/menu/tree/".$menuid);
	}

	
}
?>